package com.rqf.ls.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.rqf.ls.service.IManagementAnalysisService;
import com.rqf.ls.service.IQuestionAnswerService;
import com.server.common.chart.ChartSeries;
import com.server.common.chart.ChartSeries_Integer;
import com.server.common.chart.ColumnVectorV2;
import com.server.date.DateUtil;
import com.server.web.DataVector;
import com.server.web.RequestInput;

@Service("ManagementAnalysisService")
public class ManagementAnalysisService implements IManagementAnalysisService {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private IQuestionAnswerService questionAnswerService;
	
	@Override
	public List<Map<String, Object>> getPeriod(){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		String sqlUser = "select * from (SELECT REGISTTIME FROM NORMALUSER WHERE ABLE=1 ORDER BY REGISTTIME ASC) where rownum=1";
		Date userDate = jdbcTemplate.queryForObject(sqlUser, Date.class);
		String sqlLawyer = "select * from (SELECT REGISTTIME FROM LSUSER WHERE ABLE=1 ORDER BY REGISTTIME ASC) where rownum=1";
		Date lawyerDate = jdbcTemplate.queryForObject(sqlLawyer, Date.class);
		Date start = userDate.getTime() > lawyerDate.getTime() ? lawyerDate : userDate;
		
		return DateUtil.getMonthPeriod(start, new Date());
	}
	@Override
	public ColumnVectorV2 getUserStatictics(String startPeriod, String endPeriod) {
		String userSql = "Select * from NORMALUSER where ABLE=1 and to_char(REGISTTIME,'YYYY-MM') >= '%s' and to_char(REGISTTIME,'YYYY-MM') <= '%s'";
		String lawyerSql = "Select * from LSUSER where ABLE=1 and to_char(REGISTTIME,'YYYY-MM') >= '%s' and to_char(REGISTTIME,'YYYY-MM') <= '%s'";
		List<Map<String, Object>> userList = jdbcTemplate.queryForList(String.format(userSql, startPeriod, endPeriod));
		List<Map<String, Object>> lawyerList = jdbcTemplate.queryForList(String.format(lawyerSql, startPeriod, endPeriod));
		
		ColumnVectorV2 dataVector = new ColumnVectorV2();
		
		Map<String, Map<String, Integer>> iniDatas = this.iniDatas(startPeriod, endPeriod);
		
		Iterator<Map<String, Object>> userIterator = userList.iterator();
		Iterator<Map<String, Object>> lawyerIterator = lawyerList.iterator();
		
		while(userIterator.hasNext()){
			Map<String, Object> row = userIterator.next();
			Object timeObj = row.get("REGISTTIME");
			if (timeObj != null) {
				String timeSub = timeObj.toString().substring(0, 7);
				Map<String, Integer> map = iniDatas.get(timeSub);
				if (map == null) {
					map = new HashMap<String, Integer>();
					map.put("normal", 0);
					map.put("lawyer", 0);
				}
				Integer count = map.get("normal");
				count ++;
				map.put("normal", count);
				iniDatas.put(timeSub, map);
			}
		}
		while(lawyerIterator.hasNext()){
			Map<String, Object> row = lawyerIterator.next();
			Object timeObj = row.get("REGISTTIME");
			if (timeObj != null) {
				String timeSub = timeObj.toString().substring(0, 7);
				Map<String, Integer> map = iniDatas.get(timeSub);
				if (map == null) {
					map = new HashMap<String, Integer>();
					map.put("normal", 0);
					map.put("lawyer", 0);
				}
				Integer count = map.get("lawyer");
				count ++;
				map.put("lawyer", count);
				iniDatas.put(timeSub, map);
			}
		}
		
		int size = iniDatas.size();
		String[] xAxisData = new String[size];
		Double[] userArray = new Double[size];
		Double[] lawyerArray = new Double[size];
		
		Set<String> keySet = iniDatas.keySet();
		int i = 0;
		for(String key : keySet){
			Map<String, Integer> map = iniDatas.get(key);
			xAxisData[i] = key;
			userArray[i] = (double)map.get("normal");
			lawyerArray[i] = (double)map.get("lawyer");
			i++;
		}
		
		dataVector.setxAxisData(xAxisData);
		dataVector.addSeries(new ChartSeries("普通用户",userArray));
		dataVector.addSeries(new ChartSeries("律师用户",lawyerArray));
		
		return dataVector;
	}
	private Map<String,Map<String, Integer>> iniDatas(String startStr, String endStr){
		Map<String, Map<String, Integer>> returnMap = new LinkedHashMap<String, Map<String,Integer>>();
		int year = Integer.parseInt(startStr.substring(0, 4));
		int month = Integer.parseInt(startStr.substring(5, 7));
		
		int endYear = Integer.parseInt(endStr.substring(0, 4));
		int endMonth = Integer.parseInt(endStr.substring(5, 7));
		
		for(int i=year; i<=endYear; i++){
			int start = 1;
			int end = 12;
			if (i == year) {
				start = month;
			}
			if (i == endYear) {
				end = endMonth;
			}
			for (int j = start; j <= end; j++) {
				Map<String, Integer> map = new HashMap<String, Integer>();
				map.put("normal", 0);
				map.put("lawyer", 0);
				String tempJ = j > 10 ? j+"" : "0"+j; 
				returnMap.put(i+"-"+tempJ, map);
			}
		}
		return returnMap;
	}
	/**
	 * 普通用户和律师比例
	 * @return
	 * @author ServerZhang
	 * @date 2016年6月24日
	 */
	@Override
	public List<Map<String, Object>> pie1Data(){
		String userSql = "Select count(*) from NORMALUSER where ABLE=1";
		String lawyerSql = "Select count(*) from LSUSER where ABLE=1";
		Integer userNum = jdbcTemplate.queryForObject(userSql, Integer.class);
		Integer lawyerNum = jdbcTemplate.queryForObject(lawyerSql, Integer.class);
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		Map<String, Object> map1 = new HashMap<String, Object>();
		Map<String, Object> map2 = new HashMap<String, Object>();
		map1.put("ItemName", "普通用户");
		map1.put("Number", userNum);
		map2.put("ItemName", "律师用户");
		map2.put("Number", lawyerNum);
		list.add(map1);
		list.add(map2);
		return list;
	}
	 /**
	 * 有回答和无回答问题数比例
	 * @return
	 * @author ServerZhang
	 * @date 2016年6月24日
	 */
	@Override
	public List<Map<String, Object>> pie2Data(){
		String sql1 = "Select count(*) from QUESTION where ISABLE=1";
		String sql2 = "Select count(distinct QUESTIONID) from ANWSER where ISABLE=1";
		Integer totalNum = jdbcTemplate.queryForObject(sql1, Integer.class);
		Integer answered = jdbcTemplate.queryForObject(sql2, Integer.class);
		Integer noAnswer = totalNum - answered;
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		Map<String, Object> map1 = new HashMap<String, Object>();
		Map<String, Object> map2 = new HashMap<String, Object>();
		map1.put("ItemName", "有回复");
		map1.put("Number", answered);
		map2.put("ItemName", "无回复");
		map2.put("Number", noAnswer);
		list.add(map1);
		list.add(map2);
		return list;
	}
	/**
	 * 问题类型比例
	 * @return
	 * @author ServerZhang
	 * @date 2016年6月24日
	 */
	@Override
	public List<Map<String, Object>> pie3Data(){
		String sql = "Select QUESTIONTYPEID,count(QUESTIONTYPEID) as Num from QUESTION GROUP BY QUESTIONTYPEID ORDER BY QUESTIONTYPEID ASC";
		List<Map<String, Object>> typeList = jdbcTemplate.queryForList(sql);
		Iterator<Map<String, Object>> iterator = typeList.iterator();
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		while(iterator.hasNext()){
			Map<String, Object> map = new HashMap<String, Object>();
			Map<String, Object> next = iterator.next();
			Object typeObj = next.get("QUESTIONTYPEID");
			Object numObj = next.get("Num");
			if (typeObj != null && numObj != null) {
				Integer type = Integer.parseInt(typeObj.toString());
				String typeStr = "";
				Integer count = Integer.parseInt(numObj.toString());
				switch (type) {
				case 1:
					typeStr = "婚姻家庭";
					break;
				case 2:
					typeStr = "交通事故";
					break;
				case 3:
					typeStr = "继承纠纷";
					break;
				case 4:
					typeStr = "刑事辩护";
					break;
				case 5:
					typeStr = "劳动仲载";
					break;
				case 6:
					typeStr = "消费权益";
					break;
				case 7:
					typeStr = "消费权益";
					break;
				case 8:
					typeStr = "债权债务";
					break;
				default:
					typeStr = "其他问题";
					break;
				}
				map.put("ItemName", typeStr);
				map.put("Number", count);
				list.add(map);
			}
		}
		return list;
	}
}
